# Create your models here.
# from django.contrib.auth.models import AbstractUser
#
#
# class User(AbstractUser):
#     mobile = models.CharField(max_length=11, unique=True)
#     # 需要pillow包的支持
#     icon = models.ImageField(upload_to='icon', default='icon/default.png')
#
#     class Meta:
#         db_table = 'luffy_user'  # 指定表名
#         verbose_name = '用户表'  # 后台管理看到的中文
#         verbose_name_plural = verbose_name
#
#     def __str__(self):  # 打印对象，显示的
#         return self.username
from django.contrib.auth.hashers import make_password, check_password
from django.contrib.auth.models import AbstractUser
from django.db.models import Model, SmallIntegerField
from utils.common_models import *
from django.db import models


class Team(Model):
    id = models.AutoField(primary_key=True)
    name = CharField(max_length=32, verbose_name='编号/名字')
    expiry_time = DateTimeField(verbose_name='到期时间')
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间')
    user_quantity = IntegerField(default=10, verbose_name='用户数量')
    remark = CharField(max_length=256, blank=True, null=True, verbose_name='备注')
    enable_auto_stock_in = BooleanField(default=False, verbose_name='启用自动入库')
    enable_auto_stock_out = BooleanField(default=False, verbose_name='启用自动出库')
    is_active = BooleanField(default=True, verbose_name='激活状态')

    register_phone = CharField(max_length=32, blank=True, null=True, verbose_name='注册手机号')
    register_city = CharField(max_length=32, blank=True, null=True, verbose_name='所在城市')
    created_at = DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'erp_team'  # 指定表名
        verbose_name = '门店表'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):  # 打印对象，显示的
        return str(self.name)


class PerGroup(Model):
    """权限分组"""

    name = CharField(max_length=64, verbose_name='分组名称')

    class Meta:
        db_table = 'erp_per_group'  # 指定表名
        verbose_name = '权限分组表'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):  # 打印对象，显示的
        return self.name


class Per(Model):
    """权限"""

    group = ForeignKey(to="user.PerGroup", on_delete=models.DO_NOTHING, db_constraint=False,null=True, related_name='per', verbose_name='权限分组')
    name = CharField(max_length=64, verbose_name='权限名称')
    code = CharField(max_length=64, verbose_name='权限代码')

    class Meta:
        db_table = 'erp_per'  # 指定表名
        verbose_name = '权限表'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):  # 打印对象，显示的
        return self.name


class Role(Model):
    """角色"""

    name = CharField(max_length=64, verbose_name='名称')
    remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
    per = ManyToManyField(to="user.Per", blank=True, related_name='roles', verbose_name='权限')
    team = ForeignKey(to='Team', on_delete=models.SET_NULL, db_constraint=False,null=True, related_name='roles')

    class Meta:
        unique_together = [('name', 'team')]
        db_table = 'erp_role'  # 指定表名
        verbose_name = '角色表'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):  # 打印对象，显示的
        return self.name


class User(AbstractUser):
    """用户"""

    class Sex(TextChoices):
        """性别"""
        MAN = ('man', '男')
        WOMAN = ('woman', '女')

    type = (
        (1, '管理员'),
        (2, '老板'),
        (3, '员工'),
    )
    name = CharField(max_length=64, verbose_name='名称')
    phone = CharField(max_length=32, null=True, blank=True, verbose_name='手机号')
    sex = CharField(max_length=32, choices=Sex.choices, verbose_name='性别')
    icon = ImageField(upload_to='icon', default='icon/default.png', verbose_name='头像')
    user_type = SmallIntegerField(choices=type, default=2, verbose_name='用户类型')
    roles = ManyToManyField(to="user.Role", blank=True, related_name='users', verbose_name='角色')
    # permissions = CharField(max_length=64,default=list, verbose_name='权限')
    is_manager = BooleanField(default=False, verbose_name='管理员状态')
    created_at = DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = DateTimeField(auto_now=True, verbose_name='更新时间')

    team = ForeignKey(to='Team', on_delete=models.SET_NULL, db_constraint=False,null=True, related_name='users')

    class Meta:
        unique_together = [('username', 'team'), ('name', 'team')]
        db_table = 'erp_user'  # 指定表名
        verbose_name = '用户表'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):  # 打印对象，显示的
        return str(self.username)

    # def save(self, *args, **kwargs):
    #     if not self.pk:  # 如果是新建用户
    #         self.password = make_password(self.password)
    #     super().save(*args, **kwargs)

    @staticmethod
    def make_password(password, salt=None, hasher='default'):
        return make_password(password, salt, hasher)

    def check_password(self, raw_password):
        return check_password(raw_password, self.password)


class UserToken(Model):  # 跟User是一对一
    token = CharField(max_length=256)
    user = OneToOneField(to='User', on_delete=CASCADE, null=True)
    # user :反向，表名小写，所有有user字段


class VerificationCode(Model):
    """验证码"""

    phone = CharField(max_length=64, verbose_name='手机号')
    code = CharField(max_length=64, verbose_name='验证码')
    create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'erp_verification_code'  # 指定表名
        verbose_name = '验证码表'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):  # 打印对象，显示的
        return self.code


__all__ = [
    'Team', 'PerGroup', 'Per', 'Role', 'User', 'VerificationCode',
]
